package com.aiwhose.aiwhosewechatplublicaiwhose.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.aiwhose.aiwhosecore.bean.plublicaiwhose.bean.WXPlublicUserInfoBean;
import com.aiwhose.aiwhosecore.bean.plublicaiwhose.bean.WXPublicUserListBean;
import com.aiwhose.aiwhosewechatplublicaiwhose.utils.WXPlublicUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author tianguogang
 */
@Component
public class WXPublicUserService {
@Resource
WXPlublicUtil wxPlublicUtil;
    public WXPublicUserListBean getUserList(String nextOpenId) {
        String accessToken = wxPlublicUtil.getAccessToken();
        // 构造请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("access_token", accessToken);
        if (nextOpenId != null) {
            params.put("next_openid", nextOpenId);
        }
        String result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/user/get", params);
        WXPublicUserListBean bean = BeanUtil.toBean(JSONUtil.parseObj(result), WXPublicUserListBean.class);
        return bean;
    }
    public WXPlublicUserInfoBean getUserInfo(String openId) {
        String accessToken = wxPlublicUtil.getAccessToken();
        // 构造请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("access_token", accessToken);
        params.put("openId", openId);
        params.put("lang", "zh_CN");

        String result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/user/info", params);



        WXPlublicUserInfoBean bean = BeanUtil.toBean(JSONUtil.parseObj(result), WXPlublicUserInfoBean.class);
        return bean;
    }
}
